package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Department;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IDepartmentService;
import cn.wolfcode.crm.util.JsonResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;

    //部门视图
    @RequestMapping("/view")
    public String index() {

        return "system/department";
    }


    @RequestMapping("/selectAll")
    @ResponseBody
    public Object selectAll() {
        return departmentService.listAll();
    }

    @RequestMapping("/query")
    @ResponseBody
    public Object query(QueryObject qo) {
        return departmentService.query(qo);
    }

    @RequiresPermissions(value = {"employee:save", "员工新增/编辑"}, logical = Logical.OR)
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public Object saveOrUpdate(Department entity) {
        try {
            departmentService.saveOrUpdate(entity);
        } catch (Exception ex) {
            ex.printStackTrace();
            return new JsonResult().mark(ex.getMessage());
        }
        return new JsonResult();
    }

    // 验证用户名
    @RequestMapping("/validataName")
    @ResponseBody
    public Object validateName(String name, Long id) {

        return departmentService.validateUsernname(name, id);
    }

    //删除部门
    @RequestMapping("delete")
    @ResponseBody
    public JsonResult delete(Long id) {
        departmentService.delete(id);
        return new JsonResult();
    }

}
